const express = require('express');
const router = express.Router();
const path = require('path')
const fs = require('fs');
const userList = require('../mock/user.json');
const proList = require('../mock/list.json')

// 授权登陆：
// const db = [{
//   user: 'devin',
//   pwd: "12"
// }]

// const auth = (req, res, next) => {
//   const { user, pwd } = req.query;
//   console.log(db, 'db')
//   let flag = db.some(val => val.user == user && val.pwd == pwd)
//   if (flag) {
//     next()
//   } else {
//     res.status(401).send({ msg: '没有权限' })
//   }
// }


// 登陆接口  express把post接收的参数放置在了body上面
router.post('/login', (req, res, next) => {
  const { user, pwd } = req.body;
  let flag = userList.some((val) => val.user === user && val.pwd === pwd);
  if (flag) {
    res.send({
      code: 0,
      msg: '登陆成功'
    })
  } else {
    res.send({
      code: -1,
      msg: '登陆失败'
    })
  }
})

// 注册接口
router.post('/register', (req, res, next) => {
  const { user, pwd } = req.body;
  let flag = userList.some((val) => val.user === user);
  if (flag) {
    res.send({
      code: -1,
      msg: '账号已经存在，请重新注册'
    })
  } else {
    // 先把数据放在一个统一的数组中
    userList.push(req.body)
    // 把这个存储所有人账号密码的数组 写入到咱们的json文件中
    fs.writeFileSync('./mock/user.json', JSON.stringify(userList))
    res.send({
      code: 0,
      msg: '注册成功，请登陆'
    })
  }
})

// 增删改查  分页 搜索
// 增加数据接口  post方式
router.post('/add', (req, res, next) => {
  let length = proList.length;
  length - 1;
  proList.push({ id: length, ...req.body })
  fs.writeFileSync('./mock/list.json', JSON.stringify(proList))
  res.send({
    code: 0,
    msg: '数据增加成功',
    data: proList
  })
})

router.get('/delete', (req, res, next) => {
  const { id } = req.query;
  let deleteData = proList.filter(val => val.id != id)
  fs.writeFileSync('./mock/list.json', JSON.stringify(deleteData))
  res.send({
    code: 0,
    msg: '删除成功',
    data: deleteData,
  })
})

router.post('/update', (req, res, next) => {
  const { id } = req.body;
  // 如何将id和splice中的下标保证相等  如何根据下标删除
  proList.splice(id, 1, req.body)
  // splice(index, 1) 
  // splice(5, 1, {})
  // slice(0, 3)
  fs.writeFileSync('./mock/list.json', JSON.stringify(proList))
  res.send({
    code: 0,
    msg: '更新成功',
    data: proList,
  })
})

router.get('/page', (req, res, next) => {
  // 接收 第几页 每页多少条
  const { page, size } = req.query;
  // 重新读取数据，然后再操作；
  let listStr = fs.readFileSync('./mock/list.json').toString()
  let list = JSON.parse(listStr)
  /**
   * @param {page size  第几页  每页多少条}
   *  
   *  filter((v, index) => {
   *   page   size  下标
   *   0   *   5 =  0;  page * size
   *   1   *   5 =  5;  (page*1 + 1 * size)
   * 
   *   0 <= index < 5
   *   index >= 0 && index < 5;
   *   return index >= page * size && index < (page*1 + 1 * size)
   *  })
   * 
   */
  let pageData = list.filter((v, index) => index >= page * size && index < (page * 1 + 1) * size)
  res.send({
    code: 0,
    msg: 'success',
    data: pageData,
    total: list.length // 为了让前端计算按钮数量的
  })
})

// 搜索接口
router.get('/search', (req, res, next) => {
  const { keyword } = req.query;
  // 根据name查询所有数据
  res.send({
    code: 0,
    msg: '搜索数据',
    data: proList.filter((item, i) => item.name.includes(keyword))
  })
})


module.exports = router;